window.addEvent('domready', function(){

	/**
	 * active BAP
	 */
	new Pagination($(APP.activeBAP.pagination),{
		onPageChange: function(obj){
			$(APP.activeBAP.form).offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	new SortableTableHeader($(APP.activeBAP.table).getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','achiev_jenis','achiev_bidang','detachiev_tingkat','detachiev_nama'],
		onHeaderClick:function(item, fieldName, sort){
			$(APP.activeBAP.form).orderby.value = fieldName+" "+sort;
			search_cont($(APP.activeBAP.form));
		}
	});
	
	/**
	 * New BAP
	 */
	new Pagination($(APP.newBAP.pagination),{
		onPageChange: function(obj){
			$(APP.newBAP.form).offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	new SortableTableHeader($(APP.newBAP.table).getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','achiev_jenis','achiev_bidang','detachiev_tingkat','detachiev_nama'],
		onHeaderClick:function(item, fieldName, sort){
			$(APP.newBAP.form).orderby.value = fieldName+" "+sort;
			search_cont($(APP.newBAP.form));
		}
	});
	
	/**
	 * Counter BAP
	 */
	new Pagination($(APP.counterBAP.pagination),{
		onPageChange: function(obj){
			$(APP.counterBAP.form).offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	new SortableTableHeader($(APP.counterBAP.table).getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','achiev_jenis','achiev_bidang','detachiev_tingkat','detachiev_nama'],
		onHeaderClick:function(item, fieldName, sort){
			$(APP.counterBAP.form).orderby.value = fieldName+" "+sort;
			search_cont($(APP.counterBAP.form));
		}
	});
	
	/**
	 * Approve BAP
	 */
	new Pagination($(APP.approveBAP.pagination),{
		onPageChange: function(obj){
			$(APP.approveBAP.form).offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	new SortableTableHeader($(APP.approveBAP.table).getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','achiev_jenis','achiev_bidang','detachiev_tingkat','detachiev_nama'],
		onHeaderClick:function(item, fieldName, sort){
			$(APP.approveBAP.form).orderby.value = fieldName+" "+sort;
			search_cont($(APP.approveBAP.form));
		}
	});
	
	/**
	 * Reject BAP
	 */
	new Pagination($(APP.rejectBAP.pagination),{
		onPageChange: function(obj){
			$(APP.rejectBAP.form).offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	new SortableTableHeader($(APP.rejectBAP.table).getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','achiev_jenis','achiev_bidang','detachiev_tingkat','detachiev_nama'],
		onHeaderClick:function(item, fieldName, sort){
			$(APP.rejectBAP.form).orderby.value = fieldName+" "+sort;
			search_cont($(APP.rejectBAP.form));
		}
	});
	
	search($(APP.activeBAP.form), $(APP.activeBAP.content), $(APP.activeBAP.pagination));
	search($(APP.newBAP.form), $(APP.newBAP.content), $(APP.newBAP.pagination));
	search($(APP.counterBAP.form), $(APP.counterBAP.content), $(APP.counterBAP.pagination));
	search($(APP.rejectBAP.form), $(APP.rejectBAP.content), $(APP.rejectBAP.pagination));
	search($(APP.approveBAP.form), $(APP.approveBAP.content), $(APP.approveBAP.pagination));
});

function search(form, content, pagination){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			content.set('html', json.table_content);
			
			//rubah jumlah halamannya
			pagination.retrieve('pagination').setPageCount(Math.ceil(json.table_num_record/10));
		}
	}).send();
}

function search_cont(form, content){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			content.set('html', json.table_content);
		}
	}).send();
}

function redirectToView(bap_notif_id, bap_id, status){
	ajax(APP.utils+'/notification/updateLookBAP/'+bap_notif_id,'post','',$empty,
		function(res){
			document.location=APP.base+'/detailView/'+status+'/'+bap_id;
		});
}